<template>
	<view class="content">
		<view class="button-list">
			<button :class="showTab === 0? 'button showBtn' : ''" @click="showAll">全部商品</button>
			<button :class="showTab === 1? 'button showBtn' : ''" @click="showPhones">手机</button>
			<button :class="showTab === 2? 'button showBtn' : ''" @click="showComputers">电脑</button>
		</view>
		<view class="goods-wrap">
			<view class="goodsCard" v-for="value in goodsList" :key="value.id">
				<image :src="value.goods_icon" @click="detail(value.id)" />
				<view>
					<view class="goodInfo">{{value.goods_desc}}</view>
					<view class="price">价格：￥{{value.goods_price}}</view>
				</view>
			</view>
		</view>
	</view>
</template>
<script setup> 
	import { ref } from 'vue'
	import { getGoodsPage } from '../../api/api'

	const Photos = ref([])
	const Computers = ref([])
	const goodsList = ref([])
	const showTab = ref(0)

	function getPhotos() {
		return getGoodsPage({
			goods_category: 1,
			currentPage: 1,
			pageSize: 4
		}).then(res => {
			Photos.value = res.data;
		});
	}

	function getComputers() {
		return getGoodsPage({
			goods_category: 0,
			currentPage: 1,
			pageSize: 4
		}).then(res => {
			Computers.value = res.data;
		});
	}

	Promise.all([getPhotos(), getComputers()]).then(() => {
		showAll();
	});
			

	function showAll() {
		goodsList.value = [...Photos.value,...Computers.value]
		showTab.value = 0
	}

	function showPhones() {
		goodsList.value = Photos.value
		showTab.value = 1
	}

	function showComputers() {
		goodsList.value = Computers.value
		showTab.value = 2
	}

	function detail(id) {
		uni.setStorageSync('goods_id', id)
		uni.navigateTo({
			url: `/pages/details/details?id=${id}`
		});
	}
	
</script>
<style lang="scss">
	.content {
		padding: 20rpx;
		.button-list {
			display: flex;
			justify-content: space-between;
			button {
				margin-right: 5rpx;
				width: 100px;
				height: 30px;
				line-height: 30px;
				text-align: center;
				border-radius: 5px;
				background-color: #fff;
				color: #333;
				font-size: 14px;
				cursor: pointer;
			}
			.showBtn {
				background-color: #70ece2;
			}
		}
		.goods-wrap {
			margin-top: 20rpx;
			display: flex;
			flex-wrap: wrap;
			.goodsCard {
				border-radius: 5px;
				box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.2);
				overflow: hidden;
				width: 45%;
				margin: 15rpx 10rpx;
				image {
					width: 90%;
					height: 250rpx;
					border-top-left-radius: 5px;
				}
				.goodInfo {
					font-size: 14px;
					color: #333;
					margin: 5rpx 16rpx;
					white-space: nowrap;
					overflow: hidden;
					text-overflow: ellipsis;
				}
				.price {
					font-size: 14px;
					color: #ff5777;
					margin: 5rpx 16rpx;
				}
			}
		}
	}
</style>